package com.yangqi.ThreadDemo;

import javax.swing.plaf.TableHeaderUI;

//完成多线程龟兔赛跑 乌龟和兔子进行1000米赛跑，兔子前进5米，乌龟只能前进1米，但是兔子每前进20米就要休息500毫秒 而乌龟每100米休息500毫秒 谁先到达终点就结束程序，并显示获胜方
public class HomeWork {
    public static void main(String[] args) {
        int adv = 0;
        int total = 1000;
        Race race = new Race(total);
        Thread thread = new Thread(race);
        thread.start();
        while(true){
            adv += 1;
            System.out.println("乌龟还剩" + (total - adv));
            if (adv >= total){
                System.out.println("22222222222222222222222    乌龟获胜");
                break;
            }
            if(adv % 100 == 0){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}

class Race implements Runnable{
    private  int totalDistance;
    private static int advance = 0;

    public Race(int totalDistance) {
        this.totalDistance = totalDistance;
    }

    @Override
    public void run() {
        while(true){
            if(advance >= totalDistance){
                System.out.println("11111111111111111111111兔子到达终点");
                break;
            }
            advance += 5;
            if(advance % 20 == 0){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("兔子还剩" + (totalDistance - advance));
        }
    }
}
